import { Component, OnInit, Inject } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { UsbAuthorizeService } from '../../services/usb-authorize.service';
import { ToasterService } from '../../../share/services/toaster.service';

@Component({
  selector: 'm-wrap',
  templateUrl: './wrap.component.html',
  styleUrls: ['./wrap.component.scss']
})
export class WrapComponent implements OnInit {
  tabs = [
    { link: '/usb-device/table/restricted', name: '限制记录' },
    { link: '/usb-device/table/whitelist', name: '白名单' },
  ];
  constructor(public dialog: MatDialog) { }

  ngOnInit() {
  }

  openDialog(): void {
    const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
      width: '300px',
      height: '300px'
    });
  }

}

@Component({
  template: `
  <div class="USB-control">
    <div class="control-title">USB管控模式</div>
    <mat-radio-group [(ngModel)]="usbControl">
      <mat-radio-button value="0">可读可写，不做管控</mat-radio-button><br/>
      <mat-radio-button value="1">禁用读写，白名单放行</mat-radio-button>
    </mat-radio-group>
    <div class="footer">
      <button mat-stroked-button (click)="onNoClick()">取消</button>
      <button class="ml-2" mat-flat-button color="primary" (click)="setUsbConf()">确定</button>
    </div>

  </div>

  `,
  styleUrls: ['./wrap.component.scss']
})
export class DialogOverviewExampleDialog {

  constructor(
    public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
    private usbAuthorizeService: UsbAuthorizeService,
    private toast: ToasterService) {}

  usbControl:string;

  usbconf: any;
  ngOnInit() {
    this.usbAuthorizeService.getUsbConf().then((res:any)=>{
      this.usbControl = res.message
    })
  }

  async setUsbConf() {
    if(this.usbControl == null){
      this.toast.warn('请选择管控模式');
      return;
    }
    const params = {
      key: 'usbconf',
      value: + this.usbControl
    }
    await this.usbAuthorizeService.setUsbConf(params);
    this.usbAuthorizeService.usbEventer.emit({value: + this.usbControl})
    this.onNoClick();
  }
  onNoClick(): void {
    this.dialogRef.close();
  }

}
